#include "stdafx.h"
#include "Editor.hpp"
#include "MainFrame.hpp"

#if 0
#	include "Stackwalker.h"
#	define ALLOC_DEBUG
#endif

#ifdef ALLOC_DEBUG
class alloc
{
public:
	~alloc()
	{
		DeInitAllocCheck();
	}
} g_alloc;
#endif

namespace he
{
	IMPLEMENT_APP_NO_MAIN(Application) 

	bool Application::OnInit()
	{
		// Need to set "C" locale for numeric conversion, because wxWidgets
		// sets the default system one and then floating point conversions
		// could be broken
		wxSetlocale(LC_NUMERIC, "C");

		std::string const res_file = "1.xrc";
		// Initialize handlers
		wxXmlResource::Get()->InitAllHandlers();
		// Load resource file
		if(!wxXmlResource::Get()->Load("xrc/" + res_file))
			throw std::runtime_error("Cannot find resource file '" + res_file + "'");

		try
		{
			MainFrame *p_frame = new MainFrame();
			p_frame->Show(true);
			SetTopWindow(p_frame);
		}
		catch(std::exception const &e)
		{
			wxMessageBox(e.what());
			return false;
		}

		return true;
	}
}

int main(int argc, char* argv[])
{
#if defined(_DEBUG)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#	ifdef ALLOC_DEBUG
		InitAllocCheck(ACOutput_Advanced);
#	endif
#endif
	try
	{
		wxApp *p_app = new he::Application();
		// Initialize wxWidgets
		wxApp::SetInstance(p_app);
		// Run the application
		wxEntry(argc, argv);
		// Clean up
		wxEntryCleanup();
	}
	catch(std::exception const &e)
	{
		std::cout << e.what() << std::endl;
	}
	catch(...)
	{
		std::cout << "Undefined exception caught." << std::endl;
	}

	return 0;
}

